package com.qf.air.mq;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.air.constants.RabbitMQConstant;
import com.qf.air.service.SearchService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

/**
 * 监听AirQueue队列的消息
 */
@Component
public class AirQueueListener {

    @Autowired
    private SearchService searchService;

    @Autowired
    private ObjectMapper objectMapper;

    @RabbitListener(queues = RabbitMQConstant.AIR_QUEUE)
    public void consume(String json, Channel channel, Message message) throws IOException {
        //1. 获取routingKey判断操作
        String routingKey = message.getMessageProperties().getReceivedRoutingKey();
        //2. 判断
        switch (routingKey){
            case "air.air.save":
                // 添加操作
                searchService.add(objectMapper.readValue(json, Map.class));
                break;
            case "air.air.update":
                // 修改操作
                break;
            case "air.air.delete":
                // 删除操作
                break;
        }
    }



}
